package com.dodoke.ojserver.controller;

import com.dodoke.ojserver.bean.Users;
import com.dodoke.ojserver.bean.dto.ProblemAddDTO;
import com.dodoke.ojserver.bean.dto.UsersDTO;
import com.dodoke.ojserver.bean.valid.ProblemValidGroups;
import com.dodoke.ojserver.bean.valid.UserValidGroups;
import com.dodoke.ojserver.config.Result;
import com.dodoke.ojserver.service.IUserListService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@CrossOrigin//跨域
@RestController
@RequestMapping("/userList")
@Api(tags = "用户列表接口")
public class UserListController {

    @Autowired
    private IUserListService iUserListService;

    @GetMapping("/selectAll")
    @ApiOperation(value="获取所有用户列表信息")
    public List<Users> selectAll(){
        System.out.println("接收查询用户列表数据");
        return iUserListService.selectAll();
    }

    @GetMapping("/selectUserId")
    @ApiOperation(value="查询个人信息")
    public List<Users> selectUserId(int id){
        System.out.println("接收查询用户单条数据请求");
        return iUserListService.selectUserId(id);
    }

    @RequestMapping("/deleteUserId")
    @ApiOperation(value="单个删除用户数据")
    public String deleteUserId(int id) {
        System.out.println("接收删除用户单条数据请求");
        iUserListService.deleteUserId(id);
        return "";
    }

    @DeleteMapping("/delete/{ids}")
    @ApiOperation(value="批量删除用户")
    public Result delete(@PathVariable String[] ids) {
        iUserListService.deleteUserIdList(ids);
        return Result.success("批量删除成功", null);
    }

    @PostMapping("/add")
    @ApiOperation(value="新增用户数据")
    @ApiImplicitParam(name = "usersDTO", value = "问题新增数据传输对象", required = true, dataType = "UsersDTO")
    public Result addUser(@RequestBody @Validated(UserValidGroups.Add.class) UsersDTO usersDTO) {
        int i = iUserListService.addUser(usersDTO);
        if (i != 1){
            return Result.error(500, "新增失败");
        }
        return Result.success("新增成功", null);
    }




}
